/**
 * \file array.c
 **/

#include "array.h"
#include "mem.h"

void r_array_append(RValue array, RValue val)
{
  assert(array.type == R_VALUE_ARRAY);
  if (array.val_array->size + 1 > array.val_array->alloc_size){
    uint32_t new_size = (array.val_array->alloc_size == 0) ? 1 :
                            (array.val_array->alloc_size * 2);
    array.val_array->contents = r_mem_realloc(array.val_array->contents,
                                               sizeof(RValue) * new_size);
    array.val_array->alloc_size = new_size;
  }
  array.val_array->contents[array.val_array->size] = val;
  array.val_array->size++;
}

RValue r_array_new_sized(uint32_t alloc_size)
{
  assert(alloc_size > 0);
  RValue array;
  array.type = R_VALUE_ARRAY;
  array.val_array->alloc_size = alloc_size;
  array.val_array->size = 0;
  array.val_array->contents = r_mem_malloc(sizeof(RValue) * alloc_size);
  return array;
}
